<?php
$ROOT_DIR_PATH = '../';
require_once($ROOT_DIR_PATH . 'includes/includes.php');
require_once($ROOT_DIR_PATH . 'classes/class.category.inc.php');
$ADMIN_PAGE_TITLE = 'Category Page';
$listingUrl = $ADMIN_BASE_HREF . "categorylisting.php";

$loginUrl = $ADMIN_BASE_HREF . "login.php";
if (!$GENOBJ->isLoggedIn()) {
    $GENOBJ->redirect($loginUrl);
}

$GENOBJ->clearSessionErrorMessages();
$mode = 'insert';
$categoryObj = new category();
$currentAlias = ''; #####
if (isset($_GET['id'])) {
    if ((trim($_GET['id']) == '')) {
        $GENOBJ->redirect($listingUrl);
        exit;
    }
    $categoryObj->setid($_GET['id']);
    $mode = 'edit';
}

$categoryArray = $categoryObj->getAllCategoryList('id,title,parentid');


if (isset($_POST['id'])) {
    if (trim($_POST['id']) != '') {
        $categoryObj->setid($_POST['id']);
        $mode = 'edit';
    }
}


if ($mode == 'edit') {
    $id = $categoryObj->getid();
    $categoryData = $categoryObj->select($id);
	$currentAlias = $categoryData['alias'];  ####
    $filterArray = Array("utfdecode"); //Utf change
    $categoryData = $GENOBJ->filter($categoryData, $filterArray); //Utf change
    $categoryObj->setAll($categoryData);
}
$errorMessageArray = Array();
if ($_POST) {
    $filterElements = Array();
    $filterArray = Array("utfencode", "TrimElement", "htmlEntity");  //Utf change
    $filterElements['id'] = $_POST['id'];
    $filterElements['title'] = $_POST['title'];
	$filterElements['alias'] = $_POST['alias'];  ###
	$filterElements['parentid'] = $_POST['parentid'];
    $filterElements['sortorder'] = '0';
    $filterElements['status'] = $_POST['status'];

    $filterElements = $GENOBJ->filter($filterElements, $filterArray);
    $categoryObj->setAll($filterElements);

    if ($GENOBJ->IsEmpty($categoryObj->gettitle())) {
        $errorMessageArray[] = 'Please enter title.';
    }
	
	###
	if ($GENOBJ->IsEmpty($categoryObj->getalias())) {
        $errorMessageArray[] = 'Please enter seo friendly alias.';
    }
	
	
	

    if ($GENOBJ->IsEmpty($categoryObj->getstatus())) {
        $errorMessageArray[] = 'Please select status.';
    }

    if (trim($_POST['id']) != '') {
        $mode = 'edit';
    } else {
        $mode = 'insert';
    }
	
	#####
	if (count($errorMessageArray) <= 0) {
		if ($mode == 'insert') {
			$aliasExist = $categoryObj->checkCategoryAliasExists($categoryObj->getalias());
			if ($aliasExist) {
				$errorMessageArray[] = 'Alias name already exist';
			}
		} else {
			$aliasExist = $categoryObj->checkCategoryAlias($currentAlias, $categoryObj->getalias());
			if (!$aliasExist) {
				$errorMessageArray[] = 'Alias name already exist';
			}
		}
    }
	#####
	
    if (count($errorMessageArray) <= 0) {
        if ($mode == 'insert') {
            $categoryexist = $categoryObj->checkCategoryExists($categoryObj->gettitle());
			
            $errorMessageArray[] = 'This category name already exists. Try some other category name';
            if (!$categoryexist) {
				$categoryObj->setaddeddate(date('Y-m-d H:i:s'));
				$categoryObj->setupdateddate(date('Y-m-d H:i:s'));
			
                $insertid = $categoryObj->insert();
                if ($insertid > 0) {
                    $_SESSION['error']['category-inserted'] = 'Category Added successfully';
                    $GENOBJ->redirect($listingUrl);
                } else {
                    $_SESSION['error']['category-error'] = 'Error in insertion . Please try again!';
                    $GENOBJ->redirect($listingUrl);
                }
            }
        } else {
			$categoryObj->setupdateddate(date('Y-m-d H:i:s'));
            $categoryObj->update($id);
            $_SESSION['error']['category-updated'] = 'Category updated successfully';
            $GENOBJ->redirect($listingUrl);
        }
    }
}
?>
<?php include('templates/header.php'); ?>
<body>
    <div id="category"></div>
    <div id="ncleangrey-container">
        <?php include('templates/navigation.php'); ?>
        <div class="breadcrumbs">
            <div id="nav-icons_all">
                <ul id="nav-icons">
                    <li class="logout-icon"><a  title="Logout"  href="logout.php">Logout</a></li>
                </ul>
            </div>
            <p class="breadcrumbs">
                <a title="Home" href="dashboard.php">Home</a> &#187; <a title="Category" href="categorylisting.php">Category</a> &#187; <?php if ($mode == 'edit') { ?>Edit<?php } else { ?>Add<?php } ?> Category
            </p>
        </div>
        <div class="hstippled">&nbsp;</div>
        <div id="MainContent">
            <div>
                <div class="pageerrorcontainer-validator">
                    <div class="error-iterator"></div>
                </div>
            </div>
            <?php if (count($errorMessageArray) > 0): ?>
                <div class="pageerrorcontainer">
                <?php foreach ($errorMessageArray as $erroValue): ?>
                    <div class="pageoverflow">
                        <img src="images/icons/system/stop.gif" class="systemicon" alt="Error" title="Error" />
                        <label class="errorlabel"><?php echo $erroValue; ?></label>
                    </div>
                <?php endforeach; ?>
                </div>
            <?php endif; ?>
                    <div class="clearb">&nbsp;</div>
                    <div id="Edit_Content_Result"></div>
                    <div class="pagecontainer pageoverflow">
                        <div class="pageheader">
                    <?php if ($mode == 'edit') { ?>Edit<?php } else { ?>Add<?php } ?> Category
                    <div class="field-required">* Fields are required </div>
                </div>
                <div style="clear: both;"></div>

                <form method="post" action="category.php" name="categoryfrm" id="categoryfrm">
                    <input type="hidden" id="id" value="<?php echo $categoryObj->getid(); ?>" name="id">

                    <div id="page_content">
                        <div class="pageoverflow">
                            <div class="pagetext">Title<span class="error-star">*</span>:</div>
                            <div class="pageinput">
                                <input type="text" name="title" id="title" value="<?php echo $categoryObj->gettitle(); ?>" size="60"/>
                            </div>
                        </div>
						
						<?php #######?>
						<div class="pageoverflow">
                            <div class="pagetext">Seo Friendly Alias name<span class="error-star">*</span>:</div>
                            <div class="pageinput">
                                <input type="text" name="alias" id="alias" value="<?php echo $categoryObj->getalias(); ?>" size="60"/>
                            </div>
                        </div>
						<?php #######?>
						
						<div class="pageoverflow">
                            <div class="pagetext">Parent Category<span class="error-star">*</span>:</div>
                            <div class="pageinput">
                                <select name="parentid" id="parentid">
                                    <option value="0">--Main Category--</option>
									<?php foreach($categoryArray as $_key => $_value): ?>
									<?php if ($categoryObj->getid() != $_value['id']):?>
									<option value="<?php echo $_value['id'];?>" <?php echo ($categoryObj->getparentid() == $_value['id']) ? 'selected' : ''; ?> ><?php echo $_value['title'];?></option>
									<?php endif; ?>	
									<?php endforeach; ?>
                                </select>
                            </div>
                        </div>

                        <div class="pageoverflow">
                            <div class="pagetext">Status<span class="error-star">*</span>:</div>
                            <div class="pageinput">
                                <select name="status" id="status">
                                    <option value="">--Select Status--</option>
                                    <option value="1" <?php echo ($categoryObj->getstatus() == '1') ? 'selected' : ''; ?> >Active</option>
                                    <option value="0" <?php echo ($categoryObj->getstatus() == '0') ? 'selected' : ''; ?>>Inactive</option>
                                </select>
                            </div>
                        </div>

                        <div class="pageoverflow">
                            <p class="pagetext">&nbsp;</p>
                            <p class="pageinput">
                                <input type="submit" name="submitbutton" value="Submit"  title="Save changes" />
                                <input type="reset" name="apply" value="Reset" title="Save changes and continue to edit" />
                            </p>
                        </div>
                        <div style="clear: both;">&nbsp;</div>
                    </div>
                </form>
            </div>
            <p class="pageback">
                <a class="pageback" href="categorylisting.php">&#171; Back to Listing</a>
            </p>
            <div class="clearb"></div>
        </div>
    </div>
<?php include('templates/footer.php'); ?>
</body>
</html>
